// 引入内置模块
const fs = require("fs");
const path = require("path");
// 处理业务新闻
function news() {
  const newsFilePath = path.join(__dirname, "../data/news.json");
  const newsData = fs.readFileSync(newsFilePath, "utf-8");
  return JSON.parse(newsData);
}
// 处理业务学生
function students() {
  const studentsFilePath = path.join(__dirname, "../data/info.json");
  const studentsData = fs.readFileSync(studentsFilePath, "utf-8");
  return JSON.parse(studentsData);
}
// 抽取幸运学生
function lucyStudent() {
  const studentsArr = students();
  const num = Math.floor(Math.random() * studentsArr.length);
  studentsArr[num].count++;
  const lucyStupath = path.join(__dirname,'../data/info.json')
  fs.writeFileSync(lucyStupath,JSON.stringify(studentsArr))
  return studentsArr[num];
}
// 模块内部代码暴露
module.exports = {
  news,
  students,
  lucyStudent,
};
